package com.qf.joint.interceptor;

import com.qf.joint.pojo.Jobhunter;
import com.qf.joint.utils.SYS;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Package: com.qf.joint.interceptor
 * @ClassName: AdminLoginInterceptor
 * @Author: H
 * @CreateTime: 2020/9/15 23:29
 * @Description: 登录拦截器
 */
public class AdminLoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session = httpServletRequest.getSession();
        Object obj = session.getAttribute(SYS.USER);//User对象
        if (obj != null && obj instanceof Jobhunter) {
            //放行
            return true;
        }
        httpServletResponse.sendRedirect("/admin/adminLogin");//去后台登录界面
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

      //返回的模型与视图
        System.out.println(o + "\t" + modelAndView.getModel() + "\t" + modelAndView.getView());
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion");
    }
}
